package com.andy.nicefi.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.andy.nicefi.bean.User;
import com.andy.nicefi.dao.UserDAO;

@Repository
public class UserDAOImpl implements UserDAO{
	
	@Autowired
	private SessionFactory sessionFactory;

	public void save(User instance) {
		sessionFactory.getCurrentSession().save(instance);
	}

	public void update(User instance) {
		sessionFactory.getCurrentSession().update(instance);
	}

	public List<User> list() {
		return sessionFactory.getCurrentSession().createQuery("from User").list();
	}

	public User getObjbyId(String id) {
		User user = (User) sessionFactory.getCurrentSession().load(User.class, id);
		return user;
	}
	
	public boolean exist(String hql){
		List userlist = sessionFactory.getCurrentSession().createQuery(hql).list();
		return userlist.size() > 0;
	}

	public void delete(User instance) {
		if(instance != null)
			sessionFactory.getCurrentSession().delete(instance);
	}

	public void delete(String id) {
		User user = getObjbyId(id);
		sessionFactory.getCurrentSession().delete(user);
	}
	
	public User getValue(String hql) {
		List userlist = sessionFactory.getCurrentSession().createQuery(hql).list();
		if(userlist == null || userlist.size() <= 0)
			return null;
		return (User) userlist.get(0);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


}
